package com.company.exam8;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Xunlei {
    public static void main(String[] args) {
        /*这里是通过在运行时传入参数的方式，具体是指：右击选择run as-->run configurations-->选中运行项目，在arguments 下的第一个文本框输入要穿入得参数，*/
        if (args.length <2 || args.length > 3) {
            throw new RuntimeException("参数的个数不正确，USAGE：DOWNLOAD_PATH SAVE_PATH <THREAD_COUNT>");//这里要求传入参数有下载资源路径，文件保存路径，以及线程数
        }
        int threadCount = args.length == 2 ? 1 : Integer.parseInt(args[2]);// 下载的线程数
        new Xunlei().download(args[0], args[1], threadCount);// 开始下载
    }

    private void download(String urlStrs, String savePath, int threadCount) {
        URL url = null;
        try {
            url = new URL(urlStrs);// 要下载的地址对象协议，ip地址域，端口.资源）
        } catch (MalformedURLException e) {
            System.err.println("下载地址不正确！！");
            throw new RuntimeException("下载地址不正确", e);
        }
        String saveFilePath = getDownloadFileSavePath(urlStrs, savePath);
        long contentLenth = getContentLength(url);// 取到下载资源的长度

        // 取到每一个线程下载的长度
        long threadDownloadSize = contentLenth % threadCount == 0 ? contentLenth
                / threadCount
                : contentLenth / threadCount + 1;

        for (int i = 0; i < threadCount; i++) {
            System.out.println("启动下载线程：" + (i + 1));
            new Thread(new DownloadTask(saveFilePath, url, i
                    * threadDownloadSize, (i + 1) * threadDownloadSize - 1),
                    "下载线程" + (i + 1)).start();
        }
    }
    private long getContentLength(URL url) {
        HttpURLConnection httpConn = null;
        try {
            httpConn = (HttpURLConnection) url.openConnection();//打开连接地址，连接资源
        } catch (IOException e) {
            System.err.println("连接资源地址失败");
            throw new RuntimeException("连接资源地址失败");
        }
        long contentLength = httpConn.getContentLengthLong();// 取到下载资源的长度
        System.out.println("下载资源的长度：" + contentLength);
        return contentLength;
    }
    private String getDownloadFileSavePath(String urlStrs, String savePath) {
        int endPoint = urlStrs.lastIndexOf(".");
        int endSeperetor = urlStrs.lastIndexOf("/");
        return savePath + urlStrs.substring(endSeperetor, endPoint) + "-"
                + System.currentTimeMillis() + "-"
                + urlStrs.substring(endPoint);
    }
}

